#include<stdio.h>
#include<stdlib.h>
struct node
{
int val;
struct node *next;
};

struct node *insert(struct node *list,int num)
{
struct node *temp;

temp=malloc(sizeof(struct node));
temp->val=num;
temp->next=list;

return temp;
}
struct node *ins(struct node *list,int x, int p1)
{
int i=0;
struct node *p;

struct node *t;
t=malloc(sizeof(struct node));

p=list;
if(p1==0)
{
t->val=x;
t->next=p;
list=t;
}
else{
while(i!=p1)
{

p=p->next;
i++;
}

t->val=x;
t->next=p->next;
p->next=t;}
return list;
}
struct node *delete(struct node *list,int x)
{

struct node *p=NULL;
struct node *c;
c=malloc(sizeof(struct node));
c=list;
while(c->val !=x)
{
p=c;
c=c->next;

}
if(p== NULL)
list=list->next;
else
{
p->next=c->next;
free(c);
}
return list;
}


struct node *append(struct node *list,int x)
{
printf("c");
fflush(stdout);
struct node *temp;
struct node *p;

p=list;
temp=malloc(sizeof(struct node));
temp->val=x;
temp->next=NULL;
while((p->next)!=NULL)
{p=p->next;}

p->next=temp;
return list;
}
struct node *reverse(struct node *list)
{

struct node *p=NULL, *c ,*r=NULL;
p=malloc(sizeof(struct node));
c=malloc(sizeof(struct node));



c->val=list->val;
c->next=NULL;
p=c;
list=list->next;
while(list!=NULL)
{
r=malloc(sizeof(struct node));
r->val=list->val;
r->next=p;
p=r;
list=list->next;

}
return r;

}
void count(struct node *list,int x)
{

int c=0;
printf("c");
fflush(stdout);
while(list!=NULL)
{
if(list->val == x)
c++;list=list->next;
}
printf("%d", c);
}

void index_num(struct node *list,int x)
{
fflush(stdout);

int i=0,f=0;

while(list!=NULL)
{
if(list->val == x)
{f=1;
break;}
i++;
list=list->next;
}
if(f==1)
printf("index is%d",i);
else
printf("no. not found");
}
struct node *extend_list(struct node *list, struct node *list1)
{
struct node *p;
p=list;
while((p->next)!=NULL)
{
p=p->next;
}
p->next=list1;
return list;
}

int pop_element(struct node *list,int n,int d)
{
int i=1,p1;
struct node *p=NULL;
struct node *c;
c=malloc(sizeof(struct node));
c=list;
while(i!=d)
{
p=c;
c=c->next;
i++;
}
p1=c->val;
if(p== NULL)
list=list->next;
else
{
p->next=c->next;
free(c);
}
while(list!=NULL)
{
printf("%d\n", list->val);
list=list->next;
}
return p1;
}

void search(struct node *list, int n, int x)
{
int i=1,f=0;
while((list->next)!=NULL)
{
if(list->val==x)
{
printf("element found at position %d", i);
f=1;
break;}
i++;
list=list->next;}
if(f==0)
printf("not found");
}

struct node *sort_list(struct node *list,int n)
{
struct node *p1,*p2,*p3;
p1=malloc(sizeof(struct node));
p2=list;
p1->val=p2->val;
p3=p2->next;
while(p2->next!=NULL)
{
if(p2->val > p3->val)
{
p2->next=p3->next;
p3->next=p2;
p1->next=p3;
p2=p3->next;
p3=p2->next;
}
else
{
p1=p2;
p2=p2->next;
p3=p2->next;
}}
return list;
}
void main()
{

struct node *first=NULL;
struct node *first1=NULL;
struct node *d;
d=malloc(sizeof(struct node));
int n,i,x,p,choice,l,l1,n1;
printf("enter length: ");
scanf("%d", &l);
printf("enter elements: ");
for(i=0;i<l;i++)
{
scanf("%d",&n);
first=insert(first,n);

}
printf("enter choice: 1.append 2.reverse 3. delete 4.insert 5.count 6.index 7.extend 8.sort 9.pop 10.search  ");
scanf("%d", &choice);
switch(choice)
{
case 1:

printf("enter element to append: ");
scanf("%d",&x);
first=append(first,x);
while(first!=NULL)
{
printf("%d\n", first->val);
first=first->next;
}
break;
case 2:
d=reverse(first);
printf("reversed");
while(d!=NULL)
{
printf("%d\n", d->val);
d=d->next;
}
break;
case 3:
printf("enter element to delete: ");
scanf("%d", &x);
first=delete(first,x);
while(first!=NULL)
{
printf("%d\n", first->val);
first=first->next;
}
break;
case 4:

printf("enter number to insert: ");
scanf("%d", &x);
printf("enter position to insert: ");
scanf("%d", &p);
first=ins(first,x,p);
while(first!=NULL)
{
printf("%d\n", first->val);
first=first->next;
}
break;

case 5:
printf("enter no. t count: ");
scanf("%d", &x);

count(first,x);
break;

case 6:
printf("enter no. to find index: ");
scanf("%d", &x);
index_num(first, x);
break;

case 7:

printf("enter length: ");
scanf("%d", &l1);
printf("enter elements: ");
for(i=0;i<l1;i++)
{
scanf("%d",&n1);
first1=insert(first1,n1);
}
/*while(first1!=NULL)
{
printf("%d\n", first1->val);
first1=first1->next;
}*/
first=extend_list(first,first1);
while(first!=NULL)
{
printf("%d\n", first->val);
first=first->next;
}
case 8:
first=sort_list(first,l);
while(first!=NULL)
{
printf("%d\n", first->val);
first=first->next;
}
break;
case 9:
printf("enter position to delete element: ");
scanf("%d", &x);
p=pop_element(first,n,x);
printf("deleted element %d", p);
break;
case  10:
printf("enter element to be searched: ");
scanf("%d", &x);
search(first,n,x);
break;



}}


